/**
 * @file models/overview.js
 * @author
 */
import {routerRedux} from 'dva/router';
import api from '../api';
import Mock from 'mockjs'

export default {
  namespace : 'kpi',
  state : {
    risk: [],
    products:{},
		assetAllocation:[],
		score:{}
  },
  subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname, query}) => {
        if (pathname === '/investmentSummary/investDashboard') {
          dispatch({type: 'whetherRequest', payload: query})
        }
      })
    }
  },
  effects : {

    * whetherRequest({
      payload
    }, {put, select}) {
      yield put({type:'clearState'})
      const {lPortId, timeRange, cBenchCode, beginDate, endDate} = payload
      if (lPortId || timeRange || cBenchCode || beginDate || endDate) {
        if (lPortId && cBenchCode && ((timeRange == '0' && beginDate && endDate) || (timeRange != '0'))) {
          yield put({type: 'query', payload})
        }
      }
    },

    * query({
      payload
    }, {call, put, select}) {
      const {beginDate, cBenchCode, endDate, lPortId, timeRange} = payload
      const mockData = Mock.mock({

        "scoreMock": {
          column: [
            '排名',
            '交易员ID',
            'FDT Score',
            '交易天数',
            '日均盈利',
            '投入收益比'
          ],
          "data|1-10": [
            {
              "id|+1": 1,
              "ranking|+1": 1,
              dealerID: "@integer(0,9999)",
              score: "@float(0,100,0,3)",
              days: "@integer(1,31)",
              profit: "@integer(0,1000000)",
              ratio: "@float(0,100,0,2)%"
            }
          ]
        }
      })

      const kpi = yield call(api.getKpiData, payload)

      yield put({type:'clearState'})

      yield put({
        type: 'querySuccess',
        payload: {
          ...kpi.data,
          score:mockData.scoreMock
        }
      })

    },

  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    },
    clearState(state) {
      return {
        risk: [],
        products:{},
    		assetAllocation:[],
        score:{}
      }
    },

    querySuccess(state, {payload}) {

      const {
        risk,
        products,
        assetAllocation,
        score
      } = payload

      if (risk) {
        state.risk.push(risk)
      }

      return {
        ...state,
        products,
    		// assetAllocation,
        // score
      }
    }
  }
};
